<template>
  <el-dialog
      title="修改个人信息"
      :model-value="modelValue"
      @update:modelValue="$emit('update:modelValue', $event)"
      width="40%"
  >
    <el-form :model="form" label-width="100px" ref="formRef">
      <el-form-item label="真实姓名">
        <el-input v-model="form.realName" disabled />
      </el-form-item>

      <el-form-item label="旧密码">
        <el-input v-model="form.oldPassword" type="password" placeholder="请输入旧密码" />
      </el-form-item>

      <el-form-item label="新密码">
        <el-input v-model="form.newPassword" type="password" placeholder="不修改则留空" />
      </el-form-item>

      <el-form-item label="个人简介">
        <el-input v-model="form.profile" type="textarea" :rows="4" placeholder="请介绍您的服务优势" />
      </el-form-item>

      <el-form-item label="头像">
        <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :auto-upload="false"
        >
          <img v-if="form.avatar" :src="form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="$emit('update:modelValue', false)">取 消</el-button>
        <el-button type="primary" @click="saveProfile">保 存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'EditProfileDialog',
  props: {
    modelValue: {
      type: Boolean,
      required: true
    },
    userInfo: {
      type: Object,
      required: true
    }
  },
  emits: ['update:modelValue', 'update'],
  data() {
    return {
      form: { ...this.userInfo }
    }
  },
  watch: {
    userInfo(newVal) {
      this.form = { ...newVal };
    }
  },
  methods: {
    saveProfile() {
      this.$emit('update', this.form);
      this.$emit('update:modelValue', false);
    },
    handleAvatarSuccess(res, file) {
      this.form.avatar = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('只能上传 JPG/PNG 格式的头像');
      }
      if (!isLt2M) {
        this.$message.error('头像大小不能超过 2MB');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  display: inline-block;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 50%;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
